Mapping of Rat Sightings in Manhattan by Year

Code
# Create separate maps for each year in all boroughs
maps_all_boroughs <- list()

for (year in 2015:2019) {
  data_year <- rat_sightings_data %>%
    filter(year_created == year, !is.na(latitude), !is.na(longitude))

  maps_all_boroughs[[as.character(year)]] <- leaflet(data_year) %>%
    addTiles() %>%
    addCircleMarkers(
      lng = ~longitude,
      lat = ~latitude,
      radius = 5, 
      popup = ~paste("Location Type", location_type, "<br>Date: ", paste(month_created, year_created, sep = "-"))
    ) %>%
    addLegend("bottomright", colors = "red", labels = paste("Rat Sightings - ", year)) %>%
    addMarkers(
      clusterOptions = markerClusterOptions()
    )
}
## Assuming "longitude" and "latitude" are longitude and latitude, respectively
## Assuming "longitude" and "latitude" are longitude and latitude, respectively
## Assuming "longitude" and "latitude" are longitude and latitude, respectively
## Assuming "longitude" and "latitude" are longitude and latitude, respectively
## Assuming "longitude" and "latitude" are longitude and latitude, respectively
# Display each map separately for all boroughs and all years

names(maps_all_boroughs) <- paste("Rat Sightings -", 2015:2019)
maps_all_boroughs 
## $`Rat Sightings - 2015`
## 
## $`Rat Sightings - 2016`
## 
## $`Rat Sightings - 2017`
## 
## $`Rat Sightings - 2018`
## 
## $`Rat Sightings - 2019`
lapply(names(maps_all_boroughs), function(year) {
  maps_all_boroughs[[year]] %>%
    leaflet(width = "100%", height = "500px") %>%
    setView(lng = -73.97, lat = 40.78, zoom = 10) %>%
    addTiles() 
})
## [[1]]
## 
## [[2]]
## 
## [[3]]
## 
## [[4]]
## 
## [[5]]

2015

Code
maps_all_boroughs[["Rat Sightings - 2015"]]

2016

Code
maps_all_boroughs[["Rat Sightings - 2016"]]

2017

Code
maps_all_boroughs[["Rat Sightings - 2017"]]

2018

Code
maps_all_boroughs[["Rat Sightings - 2018"]] 

2019

Code
maps_all_boroughs[["Rat Sightings - 2019"]] 

Bar Plots: Average Annual Rat Sightings across Boroughs

Code
# Filter data for the years 2015-2019
barplot_rat_data <- rat_sightings_data %>%
  filter(year_created >= 2015 & year_created <= 2019)

# Calculate the average annual rat sightings by borough
average_sightings_by_borough <- barplot_rat_data %>%
  group_by(year_created, borough) %>%
  summarise(average_sightings = n() / length(unique(month_created)))
## `summarise()` has grouped output by 'year_created'. You
## can override using the `.groups` argument.
# Create a bar plot
ggplot(average_sightings_by_borough, aes(x = borough, y = average_sightings, fill = factor(year_created))) +
  geom_bar(stat = "identity", position = "dodge", color = "black") +
  labs(title = "Average Annual Rat Sightings by Borough (2015-2019)",
      x = "Borough",
      y = "Average Annual Rat Sightings") +
  scale_fill_manual(name = "Year", values = c("2015" = "skyblue", "2016" = "orange", "2017" = "pink", "2018" = "purple", "2019" = "red")) +
  theme_minimal()